package com.zwy.Shopping.dao.impl;

import com.zwy.Shopping.connection.DBCon;
import com.zwy.Shopping.dao.UserDao;
import com.zwy.Shopping.model.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl implements UserDao {
    //这里涉及了一个成员变量
    public int id = -1;
    /**
     * 多方式登录
     * 名字密码登录
     * 手机号登录
     * 邮箱登录
     *
     * @param name
     * @param password
     * @return User
     */
    @Override
    public User login(String name, String password) {
        //获取数据库链接
        Connection conn = DBCon.getConnection();
        //如果链接为空,则数据库连接失败
        if (conn == null) {
            System.out.println("数据库连接失败");
            return null;
        }
        try {
            //在数据库中查找,这里是多条件查询,
            PreparedStatement pst = conn.prepareStatement("select * from user where (name = ? or mobile = ? or email = ? )and password = ? ");
            pst.setString(1, name);
            pst.setString(2, name);
            pst.setString(3, name);
            pst.setString(4, password);

            //获得查询结果
            ResultSet rs = pst.executeQuery();
            //创建一个user对象用来保存查找到的对象
            User user = null;
            if (rs.next()) {
                //将查找到的数据,传入对象
                user = new User();
                user.setName(rs.getString("name"));
                user.setId(rs.getInt("id"));
                user.setEmail(rs.getString("email"));
                user.setPassword(password);
                user.setMobile(rs.getString("mobile"));
                user.setAddress(rs.getString("address"));
            }
            //释放链接资源

            rs.close();
            pst.close();
            //链接暂时不关闭
           // conn.close();
            return user;



        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public boolean login(int id, String password) {
        Connection con = DBCon.getConnection();
        try {
            PreparedStatement pst = con.prepareStatement("select * from admin where id = ? and password = ?");
            pst.setInt(1,id);
            pst.setString(2,password);
            ResultSet rs = pst.executeQuery();
            if(rs.next()){
                return true;
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } return false;
    }

    @Override
    public boolean isUserExisted(String key, String value) {
        //获取链接
        Connection con = DBCon.getConnection();
        if(con == null){
            System.out.println("数据库链接失败");
            return false;
        }
        //构建执行语句获得返回集
        try {
            PreparedStatement pst = con.prepareStatement("select * from user where  ? = ?");
            pst.setString(1,key);
            pst.setString(2,value);
            //返回集
            ResultSet rs = pst.executeQuery();
            //是否查找到
            boolean result = rs.next();
            if(result){
             this.id = rs.getInt(1);}
            //关闭连接资源
            rs.close();
            pst.close();
            //con.close();
            //返回值
            return result;

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 注册使用的用户添加方法
     *
     * @param user
     * @return
     */

    @Override
    public boolean addUser(User user) {
        boolean isAdd = false;
        //三个字段都不能重复
        if(isUserExisted("name",user.getName())||
                isUserExisted("email",user.getEmail())||
                isUserExisted("mobile",user.getMobile())){
            System.out.println("名字,邮箱号,手机号已存在");
            return  isAdd;
        }
        //获取链接
        Connection con = DBCon.getConnection();
        if(con == null){
            System.out.println("数据库连接失败");
            return isAdd;
        }
        //构造执行语句并获取添加数量
        //注意id是自增长,自己添加
        try {
            PreparedStatement pst = con.prepareStatement("insert into user(name,email,password,mobile,address)values(?,?,?,?,?)");
            pst.setString(1,user.getName());
            pst.setString(2,user.getEmail());
            pst.setString(3,user.getPassword());
            pst.setString(4, user.getMobile());
            pst.setString(5,user.getAddress());
            int rs = pst.executeUpdate();

            //如果添加的数据大于0
            if(rs > 0){
                isAdd = true;
                System.out.println("添加成功,前往数据库查看");
            }
            //关闭资源
           // con.close();
            pst.close();
            //返回值
            return isAdd;


        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        //关闭资源
    }

    /**
     * 用户个人信息修改
     * @param user
     * @return
     */
    @Override
    public boolean changeUserInfo(User user) {
        //1.根据唯一值在数据库查找,获取id
        //2.找到的数据,进行修改操作
        //3.定义修改的参数
        String name = user.getName();
        String mobile = user.getMobile();
        String address = user.getAddress();
        String email = user.getEmail();

        boolean isChange = false;
        int id = 0;
        PreparedStatement pst;
        Connection con = DBCon.getConnection();
        if(con == null){
            System.out.println("数据库连接失败");
            return isChange;
        }
        //创建执行语句,获取id
        try {
            pst = con.prepareStatement("select * from user where name = ?");
            pst.setString(1,user.getName());
            ResultSet rs = pst.executeQuery();
            if(rs.next()){
                System.out.println("已经找到对应数据位置");
                id = rs.getInt("id");
            }
            //这里应该是要排除自身
            if((isUserExisted("name",user.getName())&&(id != this.id))||
                    (isUserExisted("email",user.getEmail())&&(id != this.id))||
                    (isUserExisted("mobile",user.getMobile())&&(id != this.id))){
                System.out.println("名字,邮箱号,手机号已存在");
                return  isChange;
            }
        //创建执行语句,进行修改
            pst = con.prepareStatement("update user set name = ?, email = ?, mobile = ?,address = ? where id = ?");

            pst.setString(1,name);
            pst.setString(2,email);
            pst.setString(3,mobile);
            pst.setString(4,address);
            pst.setInt(5,id);
            int updateNum = pst.executeUpdate();
            if(updateNum > 0) {
                isChange = true;
                System.out.println("修改成功");
            }
            //关闭链接资源
            pst.close();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return isChange;
    }

    @Override
    public User getUserById(int userId) {
       Connection con = DBCon.getConnection();
       if(con == null){
           System.out.println("数据库连接失败");
           return null;
       }
        try {
            PreparedStatement pst = con.prepareStatement("select * from user where id = ?");
            pst.setInt(1,userId);
            ResultSet rs = pst.executeQuery();
            if(rs.next()){
                User user = new User();
                user.setName(rs.getString("name"));
                user.setId(rs.getInt("id"));
                user.setEmail(rs.getString("email"));
                user.setPassword(rs.getString("password"));
                user.setMobile(rs.getString("mobile"));
                user.setAddress(rs.getString("address"));
                return user;
            }
            return null;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }
}
